package com.shujia.zookeeper;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;

public class Code01ZookeeperCon {
    public static void main(String[] args) throws IOException, InterruptedException {
        /**
         *
         connectString : Zookeeper连接信息 master:2181,node1:2181,node2:2181
         sessionTimeout : 会话连接超时时间
         watcher –
         *  public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher)
         */
        ZooKeeper zooKeeper = new ZooKeeper("master:2181,node1:2181,node2:2181"
                , 10 * 1000
                , new Watcher() {
            @Override
            // 该方法在Zookeeper连接中会被触发两次，一次在建立连接时，另外一次在关闭连接时
            // 该方法执行时，是按照异步执行
            public void process(WatchedEvent event) {
                System.out.println("当前Zookeeper连接被触发...");
            }
        }
        );
        System.out.println("***********");
        zooKeeper.close();
        System.out.println("***********");


    }
}
